package com.summer.data.rs.extractor;

import com.summer.data.dialect.Dialect;
import com.summer.data.type.NocaseMetaData;
import com.summer.data.type.Union2;
import com.summer.data.type.Union3;

import java.math.BigDecimal;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 345480567@qq.com
 */
// todo: 未实现
public class KeysExtractor {

    private Integer result;

    private Map<String, Union2<Class, Object>> map = new HashMap<>();

    public Integer getResult() {
        return result;
    }

    public void setResult(Integer result) {
        this.result = result;
    }

    public void addKey(String keyName, Class keyclazz) {
        Union2<Class, Object> union3 = Union3.create(keyclazz, null);
        map.put(keyName, union3);
    }

    public String[] getKeys() {
        return map.keySet().toArray(new String[0]);
    }

    public void fetchKeys(ResultSet rs, Dialect dialect) throws SQLException {
        ResultSetMetaData metaData = rs.getMetaData();
        NocaseMetaData nocaseMetaData = new NocaseMetaData(metaData, dialect);


        for (String columnName : map.keySet()) {
            Integer pos = nocaseMetaData.findColumn(columnName);
            if (pos == null) {
                break;
            }
            int type = metaData.getColumnType(pos);

            JDBCType jdbcType;

            switch (type) {
                case Types.INTEGER:
                case Types.NUMERIC:
                    short aShort = rs.getShort(pos);
                case Types.TINYINT:
                case Types.SMALLINT:

                case Types.BIGINT:
                case Types.FLOAT:
                case Types.DOUBLE:
                case Types.REAL:
                    int anInt = rs.getInt(pos);
                    map.get(columnName).t2 = anInt;
                    break;

                case Types.VARCHAR:
                case Types.NCHAR:
                case Types.NVARCHAR:
                case Types.LONGNVARCHAR:
                case Types.CHAR:
                case Types.LONGVARCHAR:
                    String string = rs.getString(pos);
                    map.get(columnName).t2 = string;
                    break;

                case Types.DECIMAL:
                    BigDecimal bigDecimal = rs.getBigDecimal(pos);
                    map.get(columnName).t2 = bigDecimal;
                    break;



                case Types.DATE:
                case Types.TIME:
                case Types.TIMESTAMP:

                case Types.BLOB:
                case Types.BINARY:
                case Types.VARBINARY:
                case Types.LONGVARBINARY:

                case Types.CLOB:
                case Types.NCLOB:

                case Types.ROWID:
                case Types.NULL:
                case Types.BOOLEAN:
                case Types.BIT:
                case Types.OTHER:

                case Types.JAVA_OBJECT:
                case Types.DISTINCT:
                case Types.STRUCT:
                case Types.ARRAY:
                case Types.REF:
                case Types.DATALINK:
                case Types.SQLXML:
                case Types.REF_CURSOR:
                case Types.TIME_WITH_TIMEZONE:
                case Types.TIMESTAMP_WITH_TIMEZONE:
                default:
                    System.err.println("未匹配的字段类型 ： " + JDBCType.valueOf(type).getName());
                    Object object = rs.getObject(pos);
                    map.get(columnName).t2 = object;
            }
        }

    }

    public <T> T get(String keyName) {
        Union2<Class, Object> union2 = map.get(keyName);
        if (union2 != null) {
            Object val = union2.t2;

            return (T) union2.t2;
        }
        return null;
    }

}
